package com.lian.hope.mapper;

import com.lian.hope.enums.RoleType;
import com.lian.hope.model.po.Permission;
import com.lian.hope.model.po.Users;
import com.lian.hope.model.vo.OperateVO;
import com.lian.hope.model.vo.UserRoleVO;
import com.lian.hope.model.vo.UsersVO;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UsersMapper {
    @Select(
        "select u.nickname,u.id,r.role_id from users u left join user_role_relation r on u.id = r.user_id where u" +
        ".open_id =" + " " + "#{openId}")
    UserRoleVO getUserByOpenId(String openId);

    void insertUser(Users users);

    void updateUsersById(Users users);

    /**
     * 根据用户id获取用户信息
     *
     * @param userId
     * @return
     */
    @Select("select * from users where id = " + "#{userId}")
    Users getUserById(Long userId);

    @Select("update users set points = points + #{points} where id = #{userId}")
    void updateUserPointsById(Integer points, Long userId);

    /**
     * 根据角色id获取对应的权限列表
     *
     * @param roleId
     * @return
     */
    @Select(
        "SELECT p.name FROM role_permission_relation rpr JOIN permission p ON rpr.permission_id = p.id WHERE rpr" +
        ".role_id = #{roleId}")
    List<Permission> queryPermissionByRoleId(Integer roleId);

    /**
     * 新增用户角色关系
     *
     * @param id
     * @param roleType
     */
    @Select("insert into user_role_relation (user_id,role_id) values (#{id},#{roleType})")
    void insertUserRole(Long id, Integer roleType);

    @Select("select role_id from user_role_relation where  user_id = #{userId}")
    Integer getUserRoleByUserId(Long userId);

    @Select("select operate_id from role_operate where role_id = #{roleId}")
    List<Integer> getUserOperateByRole(Integer roleId);

    List<OperateVO> getOperateByIds(List<Integer> operateIds);

    @Select("select * from users where phone_number = #{phoneNumber}")
    Users getUserByPhoneNumber(String phoneNumber);
}
